package com.aizuda.boot.modules.business.department.mapper;

import com.aizuda.boot.modules.business.department.domain.response.DepartmentDetailVO;
import com.aizuda.boot.modules.business.department.domain.response.DepartmentInfoDTO;
import com.aizuda.boot.modules.business.permission.domain.entity.DepartmentEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

/**
 * @author sangyirong
 * @version 1.0
 * @date 2025/5/15
 */
@Repository
public interface DepartmentMapper extends BaseMapper<DepartmentEntity> {
    @Select("select * from auth_department where node_parent_id is null")
    DepartmentDetailVO getTotalDeparment();

    @Select("SELECT d.id, d.node_id, d.node_parent_id, d.name, d.code, d.paths, d.remark, d.sort " +
            "FROM auth_department_employee_rel rel " +
            "JOIN auth_department d ON rel.department_id = d.id " +
            "WHERE rel.employee_id = #{employeeId} " +
            "AND rel.enable = 1 AND rel.is_deleted = 0 " +
            "AND d.enable = 1 AND d.is_deleted = 0 " +
            "ORDER BY CASE WHEN rel.department_belong = 1 THEN 0 ELSE 1 END " +
            "LIMIT 1")
    DepartmentInfoDTO getMainOrFirstDepartmentByEmployeeId(@Param("employeeId") Long employeeId);
}
